package com.mellarius.activities;

import java.util.Calendar;

import com.mellarius.R;
import com.mellarius.fragments.FragmentRepository;
import com.mellarius.fragments.FragmentRepository.MainApiariesFragment;
import com.mellarius.fragments.FragmentRepository.UnfoldingSwarmingFragment;
import com.mellarius.fragments.FragmentRepository.unfoldingSwarmingEdit;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class SwarmOperation extends FragmentActivity {
	final Context context = this;
	private FragmentManager fragManager;
	private FragmentTransaction transaction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_op_colmeia);
		fragManager = getSupportFragmentManager();
	}

	/** instantiates the activity BatchOperation */
	public void swarmingFragment(View view) {

		setContentView(R.layout.activity_op_colm_swarming);
		transaction = fragManager.beginTransaction();
		transaction.setTransition(R.layout.activity_op_colm_swarming);
		transaction.replace(R.id.selectSuperColmSwarmingFrame, new FragmentRepository.UnfoldingSwarmingFragment());
		transaction.addToBackStack(null);
		transaction.commit();

	}
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.action_Apiario:
		Toast.makeText(this, MainApiariesFragment.getApiary().getIdentificacao(), Toast.LENGTH_SHORT).show();
		break;
		default:
		break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void handleFragment(View view) {

		setContentView(R.layout.activity_op_colm_handle);
		transaction = fragManager.beginTransaction();
		transaction.replace(R.id.selectSuperColmhAndleFrame, new FragmentRepository.HiveSelectorFragment());
		transaction.addToBackStack(null);
		transaction.commit();

	}
	public void transhumanceFragment(View view) {
		setContentView(R.layout.activity_op_colm_transhumance);
		transaction = fragManager.beginTransaction();
		transaction.replace(R.id.selectSuperColmtranshumanceFrame,new FragmentRepository.HiveSelectorFragment());
		transaction.addToBackStack(null);
		transaction.commit();

}
	
	
	public void editSwarming(View view) {
		if (UnfoldingSwarmingFragment.getCheckedSwarming()) {
			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.ColmSwarmingOperationsFrame, new FragmentRepository.unfoldingSwarmingEdit());
			unfoldingSwarmingEdit.setType(true);
			transaction.addToBackStack(null);
			transaction.commit();
		} else {
			UnfoldingSwarmingFragment.getDialog().show();
		}
	}

	public void createSwarming(View view) {

			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.ColmSwarmingOperationsFrame, new FragmentRepository.unfoldingSwarmingEdit());
			unfoldingSwarmingEdit.setType(false);
			transaction.addToBackStack(null);
			transaction.commit();

	}

	public void createHandle(View view) {

			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.ColmhAndleOperationsFrame, new FragmentRepository.HandleCreate());
			transaction.addToBackStack(null);
			transaction.commit();

	}
	public void createTranshumance(View view) {

		transaction = fragManager.beginTransaction();
		transaction.replace(R.id.transhumanceOperationsFrame, new FragmentRepository.TranshumanceCreate());
		transaction.addToBackStack(null);
		transaction.commit();

}
	public void datePutSwarm(int year, int month, int day) {
		EditText mEdit;
		mEdit = (EditText)findViewById(R.id.SwarmingData);
		mEdit.setText(year+"-"+month+"-"+day);	
	}
	public void datePutHandle(int year, int month, int day) {
		EditText mEdit;
		mEdit = (EditText)findViewById(R.id.handleDate);
		mEdit.setText(year+"-"+month+"-"+day);	
	}
	public void dateExtraHandle(int year, int month, int day) {
		EditText mEdit;
		mEdit = (EditText)findViewById(R.id.handleDateExtraction);
		mEdit.setText(year+"-"+month+"-"+day);	
	}


	public void selectDatePutHandle(View view) {
		DialogFragment newFragment = new SelectDateHandlePut();
		newFragment.show(getFragmentManager(), "DatePicker");
	}
	public void selectDateExtraHandle(View view) {
		DialogFragment newFragment = new SelectDateHandleExtraction();
		newFragment.show(getFragmentManager(), "DatePicker");
	}
	public void selectDateSwarm(View view) {
		DialogFragment newFragment = new SelectDateSwarm();
		newFragment.show(getFragmentManager(), "DatePicker");
	}
	@SuppressLint("ValidFragment")
	public class SelectDateSwarm extends DialogFragment implements DatePickerDialog.OnDateSetListener {

		public SelectDateSwarm() {

		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			Calendar calendar = Calendar.getInstance();
			int yy = calendar.get(Calendar.YEAR);
			int mm = calendar.get(Calendar.MONTH);
			int dd = calendar.get(Calendar.DAY_OF_MONTH) + 1;
			return new DatePickerDialog(getActivity(), this, yy, mm, dd);
		}

		@Override
		public void onDateSet(DatePicker view, int yy, int mm, int dd) {
			datePutSwarm(yy, mm + 1, dd);
		}

	}
	@SuppressLint("ValidFragment")
	public class SelectDateHandlePut extends DialogFragment implements DatePickerDialog.OnDateSetListener {

		public SelectDateHandlePut() {

		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			Calendar calendar = Calendar.getInstance();
			int yy = calendar.get(Calendar.YEAR);
			int mm = calendar.get(Calendar.MONTH);
			int dd = calendar.get(Calendar.DAY_OF_MONTH) + 1;
			return new DatePickerDialog(getActivity(), this, yy, mm, dd);
		}

		@Override
		public void onDateSet(DatePicker view, int yy, int mm, int dd) {
			datePutHandle(yy, mm + 1, dd);
		}

	}
	@SuppressLint("ValidFragment")
	public class SelectDateHandleExtraction extends DialogFragment implements DatePickerDialog.OnDateSetListener {

		public SelectDateHandleExtraction() {

		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			Calendar calendar = Calendar.getInstance();
			int yy = calendar.get(Calendar.YEAR);
			int mm = calendar.get(Calendar.MONTH);
			int dd = calendar.get(Calendar.DAY_OF_MONTH) + 1;
			return new DatePickerDialog(getActivity(), this, yy, mm, dd);
		}

		@Override
		public void onDateSet(DatePicker view, int yy, int mm, int dd) {
			dateExtraHandle(yy, mm + 1, dd);
		}

	}
	
}
